package com.easy.controller;

import com.easy.bean.Notification;
import com.easy.service.NotificationService;
import com.easy.util.Page;
import com.easy.util.Result;
import com.easy.util.SYS;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/notification")
public class NotificationController {
    @Autowired
    NotificationService notificationService;

    @PostMapping
    public Result add(@RequestBody Notification notification) {
        return Result.success(notificationService.add(notification));
    }

    @PutMapping
    public Result edit(@RequestBody Notification notification) {
        return Result.success(notificationService.edit(notification));
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        return Result.success(notificationService.del(id));
    }

    @GetMapping("/{id}")
    public Result one( HttpServletRequest request,@PathVariable("id") Integer id) {
        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 管理员可以查看所有活动，普通用户只能查看自己的活动
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                Integer userId = Integer.valueOf(idObj.toString());
                // 检查活动是否属于当前用户
                Notification existingActivity = notificationService.one(id);
                if (existingActivity != null && !existingActivity.getUser_id().equals(userId)) {
                    return Result.fail(403, "只能查看自己的活动记录");
                }
            }
        }

        return Result.success(notificationService.one(id));
    }

    @GetMapping("all")
    public Result all( HttpServletRequest request, Notification notification) {

        // 获取当前用户信息
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 普通用户只能查看自己的活动
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                notification.setUser_id(Integer.valueOf(idObj.toString()));
            }
        }
        return Result.success(notificationService.all(notification));
    }

    @GetMapping("page")
    public Result page(HttpServletRequest request, Page page, Notification notification) {
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 普通用户只能查看自己的活动
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                notification.setUser_id(Integer.valueOf(idObj.toString()));
            }
        }
        notificationService.page(page, notification);
        return Result.success(page);
    }
    @GetMapping("pageWithDetails")
    public Result pageWithDetails( HttpServletRequest request,Page page, Notification notification) {
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Object roleObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ROLE);
        Object idObj = com.easy.util.TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);

        // 普通用户只能查看自己的活动
        if (roleObj != null && !"admin".equals(roleObj.toString())) {
            if (idObj != null) {
                notification.setUser_id(Integer.valueOf(idObj.toString()));
            }
        }

        notificationService.pageWithDetails(page, notification);
        return Result.success(page);
    }
}
